﻿//刷新数据的计时器
var RefreshInterval = null;
var HCFlowChart = null; //流量图表
var PageCount = 0;
//var HCPollutantChart = null; //污染物图表

$MET.Load.Install(function () {
    ChekPageRight(2002, 4); //验证页面访问权限
    $("#TxtSearchDataTime").datebox('setValue', $MD.Date(null).toFormatString("yyyy-mm-dd"));

    var RTUrl = "../../../../WebSite/Branch/ReportForms/PollutantData/ReportHandler.ashx";
    var RTData = "action=GetArea";
    RTData += "&KeyName=";
    $MRT.Ajax(false, RTUrl, RTData, function (xmlData) {
        var Json = $MJO.GetObject(xmlData);
        var Selt = $MEM("Sel_Area");
        Selt.options.length = 0;
        for (var I in Json.Area) {
            var Op = document.createElement("OPTION");
            Op.value = Json.Area[I].EnterprisePleace;
            Op.text = Json.Area[I].PleaceName;
            Selt.add(Op);
        }
    });

    $MEM("Sel_Area").onchange = function () {
        var Group = 32767;
        var Area = $("#Sel_Area").val();
        var RTUrl = "../../../../WebSite/Branch/ReportForms/PollutantData/ReportHandler.ashx";
        var RTData = "Group=" + Group;
        RTData += "&KeyName=";
        RTData += "&Area=" + Area;
        RTData += "&action=GetPollSource";
        $MRT.Ajax(false, RTUrl, RTData, function (xmlData) {

            var Json = $MJO.GetObject(xmlData);
            if (xmlData = "nodata") {
                var Selt = $MEM("SeltPolluteSource");
                Selt.options.length = 0;
                for (var I in Json.PollSource) {
                    var Op = document.createElement("OPTION");
                    Op.value = Json.PollSource[I].PolluteSourceNo;
                    Op.text = Json.PollSource[I].PolluteSourceName;
                    Selt.add(Op);
                }

                //记录并设置上次分类选择
                //$MCC.SetValue("BeforeGroupSelect", Group);
                //触发污染源onchange事件
                $MEM("SeltPolluteSource").onchange();
            }
            else {
                $.messager.alert("提示", "无任何企业信息.");
            }
        });
    }
    $MEM("SeltPolluteSource").onchange = function () {
        var PolluteSourceNo = $MEM("SeltPolluteSource").value;
        if (PolluteSourceNo != null) {
            var RTUrl = "../../../../Service/Basic/Search/SearchDischargePort.ashx";
            var RTData = "PolluteSourceNo=" + PolluteSourceNo;
            $MRT.Ajax(false, RTUrl, RTData, function (xmlData) {
                var Json = $MJO.GetObject(xmlData);
                if (Json != null && Json.Successful != false) {
                    var Selt = $MEM("SeltDischargePort");
                    Selt.options.length = 0;
                    for (var I in Json.Data) {
                        var Op = document.createElement("OPTION");
                        Op.value = Json.Data[I].DischargePortNo;
                        Op.text = Json.Data[I].DischargePortName;
                        Selt.add(Op);
                    }
                }
                else {
                    $.messager.alert("提示", "无任何排放口信息.");
                }
            });
        }
    }

    RTUrl = "../../../../Service/Expand/RealInformation/Data/RealChart/GetPollNo.ashx";
    RTData = "";
    $MRT.Ajax(false, RTUrl, RTData, function (xmlData, state) {
        var Json = $MJO.GetObject(xmlData);

        if (Json != null && Json.Successful != false) {
            var Selt = $MEM("Sel_Item");

            Selt.options.length = 0;
            for (var I in Json.Data) {
                var Op = document.createElement("OPTION");
                Op.value = Json.Data[I].PollutantNo;
                Op.text = Json.Data[I].PollutantName;
                Op.setAttribute("TickPos", Json.Data[I].TickPos);
                Op.setAttribute("UnitName", Json.Data[I].UnitName);
                Selt.add(Op);
            }
        }
        else {
            $.messager.alert("提示", "无任何因子。");
        }
    });

    if ($MRT.QueryString("DischargePortNo") != null && $MRT.QueryString("DischargePortNo") != "") {

        var RTUrl = "../../../../Service/Basic/Search/SearchPolluteSourcePort.ashx";
        var RTData = "DischargePortNo=" + $MRT.QueryString("DischargePortNo");
        $MRT.Ajax(false, RTUrl, RTData, function (xmlData) {
            var Json = $MJO.GetObject(xmlData);
            if (Json != null && Json.Successful == true && Json.Data.length > 0) {
                $MEM("Sel_Area").onchange();
            }

            $("#Sel_Area").val(Json.Data[0].EnterprisePleace);
            $MEM("Sel_Area").onchange()
            $("#SeltPolluteSource").val(Json.Data[0].PolluteSourceNo);
            $MEM("SeltPolluteSource").onchange()
            $("#SeltDischargePort").val(Json.Data[0].DischargePortNo);

        });
    }
    else {
        $MEM("Sel_Area").onchange();
    }

    $MEM("Sel_Item").onchange = function () {
        var tic = '[' + $("#Sel_Item").find("option:selected").attr("TickPos") + ']';

        var chartSet = {
            chart: {
                renderTo: 'DivFlowChart',
                type: 'line'
            },
            credits: {
                href: '#',
                text: ''
            },
            title: {
                text: $("#SeltPolluteSource").find("option:selected").text() + $("#SeltDischargePort").find("option:selected").text() + $("#Sel_Item").find("option:selected").text() + $MD.Date($("#TxtSearchDataTime").datebox("getValue")).toFormatString("yyyy年mm月dd日")
            },
            subtitle: {
                text: null
            },
            xAxis: {
                title: {
                    text: '时间'
                },
                //设置时间间隔
                gridLineWidth: 1,

                //tickInterval: 2,
                type: 'datetime',
                labels: {
                    staggerLines: 1,
                    step: 3,
                    formatter: function () {
                        return $MD.Date(this.value).toFormatString("hh:MM");
                    }
                },
                categories: []
            },
            yAxis: {
                tickPositions: $MJO.GetObject(tic),
                title: {
                    margin: 20,
                    //rotation: 0,
                    text: $("#Sel_Item").find("option:selected").text() + '(' + $("#Sel_Item").find("option:selected").attr("unitname") + ')'
                },
                labels: {
                    formatter: function () {
                        return this.value;
                    }
                },

                minorGridLineWidth: 0,
                gridLineWidth: 1,
                alternateGridColor: null
                //plotLines: [{
                //    color: 'red',           //线的颜色，定义为红色
                //    dashStyle: 'solid',     //默认值，这里定义为实线
                //    value: 3,               //定义在那个值上显示标示线，这里是在x轴上刻度为3的值处垂直化一条线
                //    width: 2                //标示线的宽度，2px
                //}]
            },
            tooltip: {

            },
            plotOptions: {
                line: {
                    lineWidth: 2,
                    states: {
                        hover: {
                            lineWidth: 3
                        }
                    },
                    marker: {
                        enabled: false

                    }
                }
            },
            legend: {
                enabled: false
                //layout: 'vertical',
                //align: 'right',
                //verticalAlign: 'middle',
                //borderWidth: 0
            },
            exporting: {
                url: 'HighchartsExport.axd',
                filename: $("#SeltPolluteSource").find("option:selected").text() + $("#SeltDischargePort").find("option:selected").text() + $("#Sel_Item").find("option:selected").text() + $MD.Date($("#TxtSearchDataTime").datebox("getValue")).toFormatString("yyyy年mm月dd日"),
                width: 1440
            },
            series: [],
            navigation: {
                menuItemStyle: {
                    fontSize: '10px'
                }
            }
        }

        if ($("#Sel_Item").val() == 4) {
            var RedMark = $MJO.GetObject("[{ color: 'red', dashStyle: 'solid',value: 2,width: 2}]")

            chartSet.yAxis.plotLines = RedMark;
        }

        HCFlowChart = new Highcharts.Chart(chartSet);
        RefreshData();
    }
    $MEM("Sel_Item").onchange();


    //设置事件
    //$mem("btnsetinterval").onclick = function () {
    var refreshtime = 60;
    //// if (refreshinterval == null) {
    setInterval(function () {
        refreshdata();
    }, refreshtime * 1000);
    // //}
    //else {
    //    clearinterval(refreshinterval);
    //    refreshinterval = null;
    //}
    // }

    $MEM("BtnVisitDetail").onclick = function () {
        //跳转到其他页面
        $MRT.Redirect("DataDetail.html?DischargePortNo=" + $("#SeltDischargePort").val());
    }
    $MEM("BtnBackPage").onclick = function () {
        $MRT.Back(1);
    }
    //刷新数据

    $("#BtnRefresh").click(function () {
        //初始化页数
        PageCount = 0;
        $MEM("Sel_Item").onchange();
        //RefreshData();
    });

    $("#img_Left").click(function () {
        //alert(PageCount);
        //if (PageCount > 23) {
        PageCount++;
        RefreshData();
        // }
    });
    $("#img_Right").click(function () {
        if (PageCount > 0) {
            PageCount--;
            RefreshData();
        }
        else {
            $('#div_Msg').jGrowl('当前已为最新数据，系统会一分钟为您刷新一次新数据。');
        }
    });
});

//刷新数据
function RefreshData() {

    var RTUrl = "../../../../Service/Expand/RealInformation/Data/RealChart/GetPollutantData.ashx";
    var RTData = "DischargePortNo=" + $("#SeltDischargePort").val();
    RTData += "&PollutantNo=" + $("#Sel_Item").val(); //显示数据条数
    RTData += "&SearchTime=" + $("#TxtSearchDataTime").datebox("getValue");
    RTData += "&DataSum=" + PageCount; //显示数据条数
    $MRT.Ajax(true, RTUrl, RTData, function (xmlData, param) {

        var innerJson = $MJO.GetObject(xmlData);
        if (innerJson.Data == 0) {
            PageCount--;
            $('#div_Msg').jGrowl('对不起，当前已无更多过往信息。');
            //RefreshData();
            //跳出
            return;
        }
        if (HCFlowChart.series.length) {
            HCFlowChart.series[0].remove()
        }

        HCFlowChart.addSeries({
            color: Highcharts.getOptions().colors[0],
            name: $("#Sel_Item").find("option:selected").text(),
            data: []
        });
        if (innerJson != null && innerJson.Successful == true) {
            var Serie = [];
            for (var innerI in innerJson.Data) {
                //计算反序标记
                var items = [];
                var ArrayMark = innerJson.Data.length - innerI - 1;
                items.push($MD.Date(innerJson.Data[ArrayMark].GainTime).toFormatString("yyyy/mm/dd hh:MM:ss"));
                items.push($MCVT.ToDecimal(innerJson.Data[ArrayMark].Value, 3));
                Serie.push(items);
            }
            HCFlowChart.series[param].setData(Serie);
            HCFlowChart.redraw();
        }
    });
}





////设置标题信息
//var RTUrl = "../../../../Service/Basic/Search/SearchPolluteSourcePort.ashx";
//var RTData = "DischargePortNo=" + $MRT.QueryString("DischargePortNo");
//$MRT.Ajax(true, RTUrl, RTData, function (xmlData) {
//    var Json = $MJO.GetObject(xmlData);
//    if (Json != null && Json.Successful == true && Json.Data.length > 0) {
//        var PolluteSourceName = Json.Data[0].PolluteSourceName;
//        var DischargePortName = Json.Data[0].DischargePortName;
//        //设置标题信息
//        $MEM("LblSignInfo").innerHTML = PolluteSourceName + " [" + DischargePortName + "]";
//    }
//});